package cn.tedu.ivos.application.service.impl;

import cn.tedu.ivos.application.mapper.ApplicationMapper;
import cn.tedu.ivos.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos.application.pojo.entity.Application;
import cn.tedu.ivos.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos.application.service.ApplicationService;
import cn.tedu.ivos.audit.mapper.AuditMapper;
import cn.tedu.ivos.audit.pojo.entity.Audit;
import cn.tedu.ivos.audit.pojo.vo.AuditVO;
import cn.tedu.ivos.audit.service.AuditService;
import cn.tedu.ivos.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ClientInfoStatus;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Slf4j
@Service
@Transactional
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuditMapper auditMapper;
    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请单业务:applicationSaveParam={}",applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        /*遇到的问题:新增申请单对应的审批单数据时,审批数据没有申请单id
        * 原因:id是一个很特殊的属性,无法通过其他字段进行select查询
        * 解决方案:给此insert()对应的SQL上加配置useGeneratedKeys="true" keyProperty="id"
        * 效果:JDBC会为当前的application对象自动生成自增主键,并把生成的主键值自动
        * 赋值给当前application对象的id属性*/
        applicationMapper.insert(application);
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> select(ApplicationQuery applicationQuery) {
        log.debug("查询申请单业务:applicationQuery={}",applicationQuery);
        List<ApplicationVO> applicationVOS = applicationMapper.select(applicationQuery);
        for (int i = 0;i < applicationVOS.size();i++){
            ApplicationVO applicationVO = applicationVOS.get(i);
            assignAuditUserList(applicationVO);
        }
        return applicationVOS;
    }

    @Override
    public void cancel(Long id) {
        log.debug("撤销申请单业务:id={}",id);
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        //除此之外,还需要删除此申请单对应的审批单
        auditMapper.deleteByApplicationId(id);
    }

    private void assignAuditUserList(ApplicationVO applicationVO){
        List<String> auditUsernameList = new ArrayList<>();
        List<Long> auditUserIdList = new ArrayList<>();
        List<AuditVO> auditVOS = auditService.selectAuditByApplicationId(applicationVO.getId());
        for (int i = 0 ;i < auditVOS.size();i++){
            AuditVO auditVO = auditVOS.get(i);
            Long id = auditVO.getAuditUserId();
            auditUserIdList.add(id);
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String username : auditUsernameList){
            stringJoiner.add(username);
        }
        applicationVO.setAuditUsernameList(stringJoiner.toString());
        applicationVO.setAuditUserIdList(auditUserIdList);
    }
}
